Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

143
Vistas
How to save parent.child name with looping through a nested object in JavaScript? like {parent.child : "value"}

I have an object as an input with a structure like this:(basically a simple nested object)

const test ={
  id: "0000",
  parent1 : "foo",
  parent2: { child1: "data1",
             child2: "data2",
             child3: "data3" 
           },
  parent3: { child1: "data1",
             child2: "data2",
             child3:  "data3"
           } 
}

This test object has some empty values, and the purpose of the code is to loop through the object and find properties with values.

const update = {};
  for(const key in test){
    if(test[key] !== ""){
      update[key] = test[key];
      console.log(update);
    }
  };

The loop works just fine finding them. But when it finds a child, it saves it like this, as an example, {child: "data2"} and won't save the parent's name.

How is possible to modify this code and avoid too many if/else conditions, basically an optimized code, when having an object like this:

{
  id: "foo",
  parent1 : Null,
  parent2: { child1: Null,
             child2: "data2",
             child3: Null
           },
  parent3: { child1: Null,
             child2: Null,
             child3:  "data3"
           } 
}

A result like this:

{ id: "foo", parent2.child2: "data2", parent3.child3: "data3"]

or more likely something like this:

{ id: "foo", parent2:{child2: "data2"}, parent3:{child3: "data3"}}
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda